package com.sanpu.kxe.business.login.model;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.sanpu.kxe.business.login.bean.LoginOutput;
import com.sanpu.kxe.rxservice.DataUtils;
import com.sanpu.kxe.rxservice.client.LoginClient;
import com.sanpu.kxe.utils.AppContextUtil;
import com.sanpu.kxe.utils.SystemUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import rx.Observable;

/**
 * Description：登录模块model
 * Created by 薛勇军 on 2018/7/2.
 */

public class LoginModel {

    private static volatile LoginModel mInstance;

    private LoginModel() {
        if (mInstance != null) {
            throw new IllegalStateException("Already initialized.");
        }
    }

    public static LoginModel getInstance() {
        LoginModel result = mInstance;
        if (result == null) {
            synchronized (LoginModel.class) {
                result = mInstance;
                if (result == null) {
                    mInstance = result = new LoginModel();
                }
            }
        }
        return result;
    }

    /**
     * 登录接口
     * @param context     上下文
     * @param accountName 用户名
     * @param password    密码
     * @return
     */
    public Observable<LoginOutput> login(Context context, String accountName, String password) {
        JSONObject reqData = new JSONObject();
        reqData.put("userName", accountName);
        try {
            String passwordSha = DataUtils.shaEncrypt(password);
            String checkValue = ((int)(Math.random()*9000)+1000)+"";
            String field = DataUtils.encryptEncode(DataUtils.getKey(true), passwordSha, checkValue);
            reqData.put("password", URLEncoder.encode(field, "UTF-8"));
            reqData.put("checkValue", checkValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, String> map = new HashMap<>();
        map.put("system", "ANDROID");
        map.put("systemVersion", SystemUtil.getSystemVersion());
        map.put("appVersion", AppContextUtil.getVersionCode(context));
        map.put("phoneModel", SystemUtil.getSystemModel());
        reqData.put("phoneInfo", map);
        return LoginClient.getInstance().login(reqData);
    }

    /**
     * 注册
     * @param accountName   账户名
     * @param password      密码
     * @param referrerPhone 推荐人号码
     * @param smsCode       短信验证码
     * @return
     */
    public Observable<String> register(String accountName, String password, String referrerPhone, String smsCode) {
        JSONObject reqData = new JSONObject();
        reqData.put("userName", accountName);
        try {
            String passwordSha = DataUtils.shaEncrypt(password);
            String checkValue = ((int)(Math.random()*9000)+1000)+"";
            String field = DataUtils.encryptEncode(DataUtils.getKey(true), passwordSha, checkValue);
            reqData.put("password", URLEncoder.encode(field, "UTF-8"));
            reqData.put("checkValue", checkValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(referrerPhone)) {
            reqData.put("referrerPhone", referrerPhone);
        }
        reqData.put("smsCode", smsCode);
        return LoginClient.getInstance().register(reqData);
    }

    /**
     * 发送验证码
     * @param accountName 账户名
     * @param smsType     验证码类型 01：注册
     *                             02：修改登录密码
     *                             03：修改支付密码
     *                             04：实名认证
     *                             05：转账
     *                             06: 添加银行卡
     *                             07: 修改登录手机号
     * @return
     */
    public Observable<String> sendSms(String accountName, String smsType) {
        JSONObject reqData = new JSONObject();
        reqData.put("userName", accountName);
        reqData.put("smsType", smsType);
        return LoginClient.getInstance().sendSms(reqData);
    }

    /**
     * 忘记密码
     * @param accountName 账号
     * @param password    密码
     * @param smsCode     验证码
     * @return
     */
    public Observable<String> forgetPassword(String accountName, String password, String smsCode) {
        JSONObject reqData = new JSONObject();
        reqData.put("userName", accountName);
        try {
            String passwordSha = DataUtils.shaEncrypt(password);
            String checkValue = ((int)(Math.random()*9000)+1000)+"";
            reqData.put("password", DataUtils.encryptEncode(DataUtils.getKey(true), passwordSha, checkValue));
            reqData.put("checkValue", checkValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        reqData.put("smsCode", smsCode);
        return LoginClient.getInstance().forgetPassword(reqData);
    }

    /**
     * 修改密码
     * @param password    密码
     * @param smsCode     验证码
     * @return
     */
    public Observable<String> updateLoginPwd(String password, String smsCode) {
        JSONObject reqData = new JSONObject();
        try {
            String passwordSha = DataUtils.shaEncrypt(password);
            String checkValue = ((int)(Math.random()*9000)+1000)+"";
            reqData.put("password", DataUtils.encryptEncode(DataUtils.getKey(false), passwordSha, checkValue));
            reqData.put("checkValue", checkValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        reqData.put("smsCode", smsCode);
        return LoginClient.getInstance().updateLoginPwd(reqData);
    }

    /**
     * 修改支付密码
     * @param accountName 账号
     * @param password    密码
     * @param smsCode     验证码
     * @return
     */
    public Observable<String> updatePayPwd(String accountName, String password, String smsCode) {
        JSONObject reqData = new JSONObject();
        try {
            reqData.put("password", DataUtils.getMD5Str(password + accountName));
        } catch (Exception e) {
            e.printStackTrace();
        }
        reqData.put("smsCode", smsCode);
        return LoginClient.getInstance().updatePayPwd(reqData);
    }

    /**
     * 修改支付密码
     * @param phoneNumber 新登录手机号
     * @param password    密码
     * @param smsCode     验证码
     * @param idCardName  姓名
     * @param idCardNo    身份证号
     * @return
     */
    public Observable<String> modifyUserName(String phoneNumber, String smsCode, String idCardName,
                                             String idCardNo, String password) {
        JSONObject reqData = new JSONObject();
        reqData.put("phoneNumber", phoneNumber);
        reqData.put("smsCode", smsCode);
        try {
            reqData.put("idCardName", URLEncoder.encode(idCardName, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        reqData.put("idCardNo", idCardNo);
        try {
            String passwordSha = DataUtils.shaEncrypt(password);
            String checkValue = ((int)(Math.random()*9000)+1000)+"";
            String field = DataUtils.encryptEncode(DataUtils.getKey(false), passwordSha, checkValue);
            reqData.put("password", URLEncoder.encode(field, "UTF-8"));
            reqData.put("checkValue", checkValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return LoginClient.getInstance().modifyUserName(reqData);
    }
}
